package com.gofaraway.service.左神算法课;


import java.util.LinkedList;
import java.util.Random;

/**
 * 用 单向链表实现 队列和栈
 */
public class Code06_LinkedListToQueueAndStack {


    static class Node<V> {
        public V value;
        public Node<V> next;
    }


    static class MyQueue<V> {

        public Node<V> head;
        public Node<V> tail;

        public int size;

        public boolean isEmpty() {
            return this.size == 0;
        }

        public int size() {
            return this.size;
        }


        /**
         * 记得size ++
         *
         * @param v
         */
        public void offer(V v) {
            Node<V> node = new Node<>();
            node.value = v;
            if (tail == null) {
                head = node;
                tail = node;
            } else {
                tail.next = node;
                tail = node;
            }
            size++;
        }

        public V pop() {
            V ans = null;
            if (head != null) {
                ans = head.value;
                head = head.next;
                size--;
            }
            if (head == null) {
                tail = null;
            }
            return ans;
        }

        public V peek() {
            V ans = null;

            if (head != null) {
                ans = head.value;
            }
            return ans;
        }

    }


    public static void main(String[] args) {
        int testTimes = 10000;
        int maxValue = 1000;
        boolean succeed = true;
        for (int i = 0; i < testTimes; i++) {
            MyQueue<Integer> myQueue = new MyQueue<>();
            LinkedList<Integer> testQueue = new LinkedList<>();
            int numOps = new Random().nextInt(100);
            for (int j = 0; j < numOps; j++) {
                int value = new Random().nextInt(maxValue);
                int op = new Random().nextInt(3);
                if (op == 0) {
                    myQueue.offer(value);
                    testQueue.offer(value);
                } else if (op == 1 && !myQueue.isEmpty()) {
                    if (!myQueue.pop().equals(testQueue.pollFirst())) {
                        succeed = false;
                        break;
                    }
                } else if (op == 2 && !myQueue.isEmpty()) {
                    if (!myQueue.peek().equals(testQueue.peekFirst())) {
                        succeed = false;
                        break;
                    }
                }
            }
            if (!succeed) {
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");
    }


    /**
     * todo 单链表实现一个 栈结构
     */
    static class MyStack{
        public  int size;



    }

}
